﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Syndication;
using System.Text;
using System.Xml;
using AzureProvider.MiniSensor;

namespace AzureProvider.MiniSensor
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    public class RssChannel : IHttpHandler
    {
        public Guid channelId;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/rss+xml";
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.BufferOutput = false;

            if (!String.IsNullOrEmpty(context.Request["ChannelId"]))
            {
                List<Message> Msgs=null;
                Channel Ch=null;
                try
                {
                    channelId = new Guid(context.Request["ChannelId"]);

                    Messages msgs_get = new Messages();
                    Msgs = msgs_get.GetMessages(channelId);
                    Channels ch_get = new Channels();
                    Ch=ch_get.SelectChannel(channelId);
                }
                catch
                {
                }
                if (Msgs != null && Ch != null)
                {
                    // Create a new Syndication Feed.
                    SyndicationFeed feed = new SyndicationFeed(Ch.Name, "Contenu du canal " + Ch.Name, null);

                    var items = Msgs.Select(
                        Msg => new SyndicationItem(
                                    Msg.DateCreated.ToLongTimeString(), 
                                    Msg.Content,
                                    new Uri("http://www.zecloud.fr/channel.aspx?channelid="+channelId.ToString())
                                      )).ToList();
                    feed.Items = items;
                    string query = context.Request["format"];
                    SyndicationFeedFormatter formatter = null;
                    if (query == "atom")
                    {
                        formatter = new Atom10FeedFormatter(feed);
                    }
                    else
                    {
                        formatter = new Rss20FeedFormatter(feed);
                    }
                    System.Xml.XmlWriter Writer = System.Xml.XmlWriter.Create(context.Response.OutputStream);
                    formatter.WriteTo(Writer);
                    Writer.Flush();
                    context.Response.End();
                }
            }
            else
            {
                Channels ch_get = new Channels();
                List<Channel> chs = ch_get.GetListChannels();
                // Create a new Syndication Feed.
                SyndicationFeed feed = new SyndicationFeed("Canaux Zecloud", "Liste des canaux ", null);

                var items = chs.Select(
                    ch => new SyndicationItem(
                                ch.Name,
                                ch.Name,
                                new Uri("http://www.zecloud.fr/channel.aspx?channelid=" + ch.Id.ToString())
                                  )).ToList();
                feed.Items = items;
                string query = context.Request["format"];
                SyndicationFeedFormatter formatter = null;
                if (query == "atom")
                {
                    formatter = new Atom10FeedFormatter(feed);
                }
                else
                {
                    formatter = new Rss20FeedFormatter(feed);
                }
                System.Xml.XmlWriter Writer = System.Xml.XmlWriter.Create(context.Response.OutputStream);
                formatter.WriteTo(Writer);
                Writer.Flush();
                context.Response.End();
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
